(function() {

	$.utils = {
		formFilter : function(paramObj, filterValue) {
			filterValue = filterValue || 'null';
			var map = {};

			for ( var key in paramObj) {
				var value = paramObj[key];
				if (value === 'null') {
					map[key] = '';
				} else {
					map[key] = this.string.replaceAll(paramObj[key], '-', '');
				}
			}
			return map;
		},
		formArrayToObj : function(form) {
			var array = $(form).serializeArray();
			return $.utils.arrayToObj(array, form);
		},
		arrayToObj : function(array, form) {
			var obj = {};
			if (array.length > 0) {
				for ( var i = 0; i < array.length; i++) {
					var a = array[i];

					var _c = $(form).find('[name="' + a.name + '"]');
					if (_c && _c.attr('boInput')) {
						obj[a.name] = $.trim($.utils.encodeUTF8(a.value));
					} else {
						obj[a.name] = $.trim(a.value);
					}
				}
			}

			return obj;
		},
		formSerizableFilter : function(form, filterValue) {
			filterValue = filterValue || 'null';
			return $(form).not('[value="' + filterValue + '"]').serialize();
		},
		string : {
			replaceAll : function(str, text, replacement) {
				return str.replace(new RegExp(text, 'gm'), replacement);
			}
		},
		setHiddenInput : function(name, value, formId) {
			formId = formId || 'queryForm'
			if (value) {
				var hidden = $('input[name="' + name + '"]');
				if (hidden.length == 0) {
					hidden = $('<input type="hidden" id="' + name + '" name="'
							+ name + '"/>');
					$('#' + formId).append(hidden);
				}
				hidden.attr('value', value);
			}
		},
		noData : {
			icons : {
				table : 'table',
				column : 'histogram',
				pie : 'PieChart',
				line : 'trend',
				area : 'AreaChart',
				bubble : 'BubbleChart',
				dashboard : 'dashboard',
				radar : 'radar'
			},
			showNoDataInfo : function(el, noData) {

				if (!noData.enable) {
					return;
				}

				$(el).hide();
				var parent = $(el).parent();

				var box = parent.find('div.nodataBox');
				if (box.length > 0) {
					box.show();
					return;
				}

				var nodataBox = $('<div class="nodataBox">');
				if (noData.css) {
					nodataBox.css(noData.css);
				}

				var nodata = $('<div class="nodata"></div>');
				var nodatInfo = $('<div class="nodata-info"></div>');
				var nodatP = $('<p></p>');

				parent.append(nodataBox);
				nodataBox.append(nodata);

				nodatInfo.append(nodatP);

				var icon = noData.icon;
				var icons = $.utils.noData.icons;
				icon = icons[icon]
				if (!icon) {
					icon = icons('table');
				}

				var nodataIcon = $('<i class="' + icon + '-nodata  icon-nodata"></i>');
				nodata.append(nodataIcon);
				nodata.append(nodatInfo);
				var nodataDesc = '对不起，没有查询到对应的数据。<br />建议你换个查询条件或';
				var link = $('<a href="javascript:void(0);">刷新</a>');
				link[0].targetEl = noData.target || el;

				link.click(function() {
					var target = $(this)[0].targetEl;
					var refresh = noData.refresh;
					if ($.isFunction(refresh)) {
						refresh.call(target);
					}
				});
				nodatP.append(nodataDesc);
				nodatP.append(link);
			},
			hideNoDataInfo : function(el) {
				$(el).parent().find('div.nodataBox').hide();
				$(el).show();
			}
		},
		loading : {
			show : function(el) {// 显示loading mask
				el = $(el);

				var maskDiv = el[0].maskDiv;

				el.find(maskDiv).remove();
				// 生产loading图片
				var loadingImg = (jsContextPath || '') + '/images/loading.gif';
				var div = $('<div><img src="' + loadingImg + '" /></div>')
						.appendTo(el);
				var parent = el.parent();
				div.css( {
					'text-align' : 'center',
					'vertical-align' : 'middle',
					'background' : '#FFFFF',
					'height' : parent.height(),
					'width' : parent.width(),
					'display' : 'table-cell',
					'border' : '1px solid #EDEDED'
				});
				// 缓存loading
				el[0].maskDiv = div;
			},
			hide : function(el) {// 隐藏loading mask
				var maskDiv = $(el)[0].maskDiv;
				$(el).find(maskDiv).remove();
			}
		},
		obj2str : function(obj) {
			return this.obj2Array(obj, '').join('&');
		},
		params2obj : function(obj) {
			return arr2obj(this.obj2Array(obj, ''));

			function arr2obj(arr) {
				var obj = {};
				for ( var i in arr) {
					var a = arr[i].split('=');
					var key = a[0], value = a[1];
					obj[key] = value;
				}
				return obj;
			}

		},
		obj2Array : function(obj, prev) {
			return obj2uriArray(obj, prev);

			function obj2uriArray(obj, prev) {
				var type = $.type(obj);
				var ret = [];
				if (type == 'object') {
					for ( var field in obj) {
						var value = obj[field];
						if (isSimpleValue(value)) {
							var quot = (prev && prev != '') ? '.' : '';
							ret.push(prev + quot + field + '='
									+ encodeURI(value));
						} else {
							var subRets = obj2uriArray(obj[field],
									(prev ? prev + '.' : '') + field);
							for ( var i = 0; i < subRets.length; i++) {
								ret.push(subRets[i]);
							}
						}
					}
				} else if (type == 'array') {
					for ( var i = 0; i < obj.length; i++) {
						var value = obj[i];
						if (isSimpleValue(value)) {
							ret.push(prev + '[' + i + ']=' + encodeURI(value));
						} else {
							var subRets = obj2uriArray(obj[i], prev + '[' + i
									+ ']');
							for ( var j = 0; j < subRets.length; j++) {
								ret.push(subRets[j]);
							}
						}
					}
				}

				return ret;
			}

			function isSimpleValue(obj) {
				return $.type(obj) === "boolean" || $.type(obj) === "number"
						|| $.type(obj) === "string" || $.isFunction(obj);
			}
		},
		updateChartAll : function(chart, config) { // 刷新页面所有chart
			config = config || {}; // geneChart config参数
			chart = chart || $(".chart"); // fusionchart
			if (chart) {
				$(chart).each(function() {
					$(this).updateChart(config);
				});
			}
		},
		updateGridAll : function(grid, config) { // 刷新页面所有chart
			config = config || {}; // grid config参数
			grid = grid || $(".grid"); // fusionchart
			if (grid) {
				$(grid).each(function() {
					$(grid).jqGrid('setGridConfig', {
						paramObj : config
					});
					$(grid).doFind();
				});
			}
		},
		updateAllBo : function(bo, url) { // 刷新页面所有chart
			config = config || {}; // grid config参数
			bo = bo || $(".chart"); // fusionchart
			if (bo) {
				$(bo).each(function() {
					$(this).attr('src', url);
				});
			}
		},
		openFullWin : function(url) { // 根据URL打开一个全屏窗口
			var w = 1280 - 10, h = 768 - 30;
			if (window.screen) {
				w = screen.availWidth - 10;
				h = screen.availHeight - 30;
			}
			
			var win = window
					.open(
							url,
							"",
							"width="
									+ w
									+ ",height="
									+ h
									+ "menubar=1,toolbar=0,resizable=0,titlebar=1,left=0,top=0,directories=0,location=0,status=0,scrollbars=0");
		},
		/** 获取报表顶层元素 */
		getChartRootEL : function(elId) {
			var el;
			if (elId) {
				var arr = elId.split("_");
				var rootId = arr[0];
				if (rootId) {
					el = $("#" + rootId);
				}
			}
			return el;
		},
		/** 获取表单值 */
		getFormData : function(formId) {
			var data;
			formId = formId || 'queryForm'
			data = $.utils.formArrayToObj($('#' + formId));
			for(var i in data){
				if($('#'+i).attr('allowserialize')=='no'){
					data[i]='';
				}
			}
			return data;
		},
		getParamsMap : function() {// 获取url的参数，并转成map
			var search = window.location.search;
			search = search.replace('?', '');
			var arr = search.split('&');
			var map = {};
			var reg = new RegExp('^lsS');

			for ( var i in arr) {
				var o = arr[i];
				var param = o.split('=');
				var name = param[0];
				name = name.replace(reg, '');
				var value = param.length > 1 ? param[1] : '';
				map[name] = value;
			}
			return map;
		},
		setFormParams : function(paraMap, el) {// 设置参数到form元素里
			$el = $(el || '#queryForm');

			for ( var key in paraMap) {
				var obj = $el.find('*[name="' + key + '"]');
				var paraValue = paraMap[key];
				if (0 === obj.length) {
					var hidden = $('<input type="hidden"/>');
					hidden.attr( {
						'id' : key,
						'name' : key,
						'value' : paraValue
					});
					$el.append(hidden);
				} else {
					obj.val(paraValue);
				}
			}
		},
		encodeUTF8 : function(value) {// 转为utf-8编码
			if (value) {
				return encodeURI('%' + value + '%');
			}
		},
		isZN : function(value) { // 判断是否是中文
			return (/[\u4E00-\u9FA5]/i.test(value));
		},
		gridCellSpan : function(rowSpan, rowIdx) {

			if (!rowIdx) {
				return '';
			}

			var css = '';
			if (1 === rowIdx) {
				css = ' style="background-color: #ffffff;word-wrap: break-word;text-align:center;border-bottom:1px solid #ddd9d7;"' + ' rowSpan=' + rowSpan
				return css;
			} else {
				css = ' style="display:none;"';
			}
			return css
		},
		widgetLinkage : function(widget) {
			var readyList = [];
			var id = $(widget).attr('id');
			// 遍历select元素
		$('select')
				.each(
						function() {
							var selfOpt = $(this).getSelOption(), masters = selfOpt.masters
									|| [];
							if (isContainsItem(masters, id)) {
								readyList.push(selfOpt);
							}

						});

		// 遍历input元素
		$('input').each(function() {
			var masters = $(this).attr('masters') || '';
			masters = masters.split(';');

			if (isContainsItem(masters, id)) {
				$(this).val('');
				var hidden = $(this).getRealInput();
				hidden.val('');
			}
		})

		$.select.load(null, null, readyList);

		function isContainsItem(array, item) {

			for ( var i = 0; i < array.length; i++) {
				if (item === array[i]) {
					return true;
				}
			}
			return false;
		}
		;
	},
	uuidCompact : function() {// 生成UUID
			return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g,
					function(c) {
						var r = Math.random() * 16 | 0, v = c == 'x' ? r
								: (r & 0x3 | 0x8);
						return v.toString(16);
					});
		},
		uuidCompactWithoutRod:function(){
			return 'xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx'.replace(/[xy]/g,
					function(c) {
						var r = Math.random() * 16 | 0, v = c == 'x' ? r
								: (r & 0x3 | 0x8);
						return v.toString(16);
					});
		},	
		urlProccesor : function(url, paramStr) {
			if (url && paramStr) {
				var qs = '?';
				paramStr = paramStr.replace(/^&/,'');
				if (url.indexOf(qs) > -1) {
					url += '&' + paramStr;
				} else {
					url += qs + paramStr;
				}
			}
			return url;
		},
		mergeCellPager:function (data){//合并修改分页信息
			var all=data.records/2;
			var start,end,s;
			
			if(data.page==data.total){
				s=(data.records-data.rows.length)/2;
				end=data.records/2;
				start=s*(data.page-1)+1;
			}else{
				s=data.rows.length;
				end=s*data.page/2;
				start=s*(data.page-1)/2+1;
			}
			
			var pageInfo=start+ ' - ' +end+' 共 ' +all+ ' 条';
		    $('.ui-paging-info').html(pageInfo);
		}
	};
})();
